package com.glasses.business.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.glasses.business.R;
import com.glasses.business.entity.SelectItem;
import com.glasses.business.inter.OnItemClick;

import net.neiquan.applibrary.flowtag.OnInitSelectedPosition;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HanHailong on 15/10/19.
 */
public class TagAdapter<T> extends BaseAdapter implements OnInitSelectedPosition {

    private final Context mContext;
    private final List<T> mDataList;

    public TagAdapter(Context context) {
        this.mContext = context;
        mDataList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_tag, null);
        final TextView textView = (TextView) view.findViewById(R.id.tv_tag);
        if (mDataList != null && mDataList.size() > 0) {
            final SelectItem.ValueBean valueBean = (SelectItem.ValueBean) mDataList.get(position);
            textView.setText(valueBean.getValue());
            if (valueBean.isSelected()) {
                textView.setBackgroundResource(R.drawable.round_solid_red_);
                textView.setTextColor(mContext.getResources().getColor(R.color.white));
            } else {
                textView.setBackgroundResource(R.drawable.round_solid_gray_);
                textView.setTextColor(mContext.getResources().getColor(R.color.text_gray));
            }
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClick.onItemClick(0,valueBean);
//                    valueBean.setSelected(!valueBean.isSelected());
//                    if(valueBean.isSelected()){
//                        textView.setBackgroundResource(R.drawable.round_solid_red_);
//                        textView.setTextColor(mContext.getResources().getColor(R.color.white));
//                    }else{
//                        textView.setBackgroundResource(R.drawable.round_solid_gray_);
//                        textView.setTextColor(mContext.getResources().getColor(R.color.text_gray));
//                    }
                }
            });
        }
        return view;
    }

    private OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public void onlyAddAll(List<T> datas) {
        mDataList.addAll(datas);
        notifyDataSetChanged();
    }

    public void clearAndAddAll(List<T> datas) {
        mDataList.clear();
        onlyAddAll(datas);
    }

    @Override
    public boolean isSelectedPosition(int position) {
        if (position % 2 == 0) {
            return true;
        }
        return false;
    }
}
